package com.yty.controller;


import com.yty.annotation.LogOperation;
import com.yty.page.PageResult;
import com.yty.param.ClazzQueryParam;
import com.yty.pojo.Clazz;
import com.yty.pojo.Result;
import com.yty.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    @GetMapping
    public Result classList(ClazzQueryParam param){
        log.info("查询班级列表，参数：{}",param);
        PageResult clazzes = clazzService.findAll(param);
        log.info("查询班级列表成功:{}", clazzes);
        return Result.success(clazzes);
    }

    @GetMapping("/{id}")
    public Result findClazzById(@PathVariable("id") Integer id){
        log.info("查询班级，参数：{}",id);
        Clazz clazz = clazzService.findClazzById(id);
        log.info("查询班级成功:{}", clazz);
        return Result.success(clazz);
    }

    @LogOperation
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        log.info("添加班级，参数：{}",clazz);
        int id = clazzService.addClazz(clazz);
        if (id == 0) {
            return Result.error("添加班级失败");
        }
        log.info("添加班级成功id为{}", id);
        return Result.success();
    }
    @LogOperation
    @DeleteMapping("/{id}")
    public Result deleteClazzById(@PathVariable("id")  Integer... id){
        log.info("删除班级，参数：{}",id);
        int i = clazzService.deleteClazzById(id);
        if (i == 0) {
            return Result.error("删除班级失败,班级存在有学生");
        }
        log.info("删除班级成功数量----:{}",i);
        return Result.success();
    }
    @LogOperation
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        log.info("修改班级，参数：{}",clazz);
        int i = clazzService.updateClazz(clazz);
        if (i == 0) {
            return Result.error("修改班级失败");
        }
        return Result.success();
    }
    @GetMapping("/list")
    public Result findAll(){

        List<Clazz> clazzes = clazzService.findAllList();
        log.info("查询班级列表成功:{}", clazzes);
        return Result.success(clazzes);
    }


}
